// pages/shopCar/shopCar.js
const db = wx.cloud.database();
const shopCar = db.collection("shopCar");

Page({

  /**
   * 页面的初始数据
   */
  data: {
    shopCar: [],
    choosedTemp: false,
    choose: [],
    allChoosed:false,
    editOrder:false,
    price:0,
    allChoosedGoods:[],
    //所有商品的价格
    sumPrice:0,
    allPrice:[],
    openId:''
  },
 

  onLoad: function(options) {
    var that=this;
    //获取购物车信息
    wx.cloud.callFunction({
      name:'checkUser'
    }).then(res=>{
      console.log(res.result.openid)
      this.setData({
        openId:res.result.openid
      })
    }).catch(console.error)

    wx.cloud.callFunction({//调用云函数
      name: "checkOpenId",// 要调用的云函数名称
      data:{
        dbName:'shopCar'
      }
    }).then(res=>{
      console.log(res.result.data)
      this.setData({
            shopCar: res.result.data,
         })
    })
      //获取所有商品的价格
      shopCar.field({
        price:true
      }).get().then(res => {
        console.log(res.data)
        this.setData({
          allPrice:res.data
        })
      })
    that.calPrice(),
    that.changeAllChooseStatus()
  },
  
  //获取选中状态数组的价格
  calPrice() {
    wx.cloud.callFunction({//调用云函数
      name: "checkShopCarChoosed"// 要调用的云函数名称
    }).then(res=>{
      console.log(res.result.data)
      this.setData({
        allChoosedGoods: res.result.data,
        price:0
      })
      console.log(this.data.allChoosedGoods.length)
      for (var i = 0; i < this.data.allChoosedGoods.length; i++) {
        this.data.price+=this.data.allChoosedGoods[i].price*100*this.data.allChoosedGoods[i].num;
        console.log(this.data.price)
      }
      this.setData({
        price: this.data.price
      })
    })
  },
  // 看是否是全选的状态
  changeAllChooseStatus(){
    //获取choose数组
    wx.cloud.callFunction({//调用云函数
      name: "choosedGoodsColumn"// 要调用的云函数名称
    }).then(res=>{
      console.log(res.result.data)
      this.setData({
        choose:res.result.data
      })
      for (var i = 0; i < this.data.choose.length; i++) {
        if (this.data.choose[i].choosed == false) {
          this.setData({
            allChoosed: false
          })
          return;
        }
        this.setData({
          allChoosed: true
        })
      }
    })
  },

  //点击编辑
  onEditTap:function(event){
    if(this.data.editOrder==false){
      this.setData({
        editOrder: true
      })
    }
    else{
      this.setData({
        editOrder: false
      })
    }
    
  },

  //增加数量
  onChangePlus(event) {
    const _ = db.command;
    var itemId = event.currentTarget.dataset.itemid;
    console.log(event);
    console.log(itemId)
    shopCar.doc(itemId).update({
      data: {
        num: _.inc(1)
      },
      success: function(res) {
        console.log(res)
      }
    })
   this.calPrice()
  },
  //减少数量
  onChangeMinus(event) {
    const _ = db.command;
    var itemId = event.currentTarget.dataset.itemid;
    console.log("detail" + event.detail);
    console.log(itemId)
    shopCar.doc(itemId).update({
      data: {
        num: _.inc(-1)
      },
      success: function (res) {
        console.log(res)
      }
    })
    this.calPrice()
  },

  //选中or未选中
  onChooseTap: function(event) {
    var itemId = event.currentTarget.dataset.id;
    var itemIndex = event.currentTarget.dataset.index;
    console.log(itemId + "  " + itemIndex);

    shopCar.doc(itemId).get().then(res => {
      var price=this.data.price;
      console.log(res.data.choosed)
      if (res.data.choosed == false) {
        this.data.choose[itemIndex].choosed = true;
        console.log(this.data.choose)
        this.setData({
          choose: this.data.choose
        })
        console.log(this.data.choose[itemIndex].choosed)
        shopCar.doc(itemId).update({
          data: {
            choosed: true,
          }
        })
      } else {
        this.data.choose[itemIndex].choosed = false;
        console.log(this.data.choose)
        this.setData({
          choose: this.data.choose
        })
        shopCar.doc(itemId).update({
          data: {
            choosed: false
          }
        })
      }
      this.calPrice();
      this.changeAllChooseStatus();
    })
  },
  //全选
  onAllChoosedTap:function(event){
    var that=this;
    var chooseTemp=this.data.choose;
    var priceSum=0;
    //将所有未选中的设置为选中
    if(this.data.allChoosed==false){
      for (var i = 0; i < chooseTemp.length;i++){
        var obj = chooseTemp[i];
        obj.choosed=true;
      }
      this.setData({
        allChoosed: true,
        choose: chooseTemp
      })
  
      wx.cloud.callFunction({
        name:'updateAllChoosed',
        data:{
          ostates:false,
          nstates:true
        }
      }).then(res=>{
        //console.log(res.result)
        this.calPrice();
      }).catch(console.error)
    }
    else{
      for (var i = 0; i < chooseTemp.length; i++) {
        var obj = chooseTemp[i];
        obj.choosed = false; 
      }
      this.setData({
        allChoosed:false,
        choose: chooseTemp,
        price:0
      })
      wx.cloud.callFunction({
        name: 'updateAllChoosed',
        data: {
          ostates: true,
          nstates: false
        }
      }).then(res => {
        //console.log(res.result)
      }).catch(console.error)
    }
  },
  //提交订单
  onClickSubmitButton:function(event){
    wx.navigateTo({
      url: 'orderDetail/orderDetail?price='+this.data.price
    })
  },
  onClickDeleteButton:function(event){
    var that=this;
    wx.showModal({
      title: '删除',
      content: '确认删除吗？',
      success:function(res){
        if(res.confirm){
          wx.cloud.callFunction({
            name:'deleteBatchCard',
            data:{
              dbName:'shopCar'
            }
          }).then(res=>{
            console.log(res.result)
            that.onShow();
          }).catch(console.error)
         
        }else if(res.cancel){
          console.log("用户点击了取消");
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    var that=this; 
    this.getTabBar().init();
    wx.cloud.callFunction({//调用云函数
      name: "checkOpenId",// 要调用的云函数名称
      data:{
        dbName:'shopCar'
      }
    }).then(res=>{
      console.log(res.result.data)
      that.setData({
            shopCar: res.result.data,
         })
    })
    
    this.calPrice();
    wx.cloud.callFunction({//调用云函数
      name: "choosedGoodsColumn"
    }).then(res=>{
      console.log(res.result.data)
      that.setData({
        choose:res.result.data
      })
    })
    this.changeAllChooseStatus();
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})